<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="工序">
          <el-select
            v-model="searchParam.processCode"
            placeholder="请选择工序"
            clearable
            filterable
            @change="processChange"
          >
            <el-option
              v-for="item in processCodeList"
              :key="item.processCode"
              :label="item.processName"
              :value="item.processCode"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="生产线">
          <el-select
            v-model="searchParam.processEquipId"
            placeholder="请选择生产线"
            clearable
            filterable
            @change="processEquipChange"
          >
            <el-option
              v-for="item in processEquipList"
              :key="item.id"
              :label="item.equipCode"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="设备">
          <el-select
            v-model="searchParam.deviceId"
            placeholder="请选择设备"
            clearable
            filterable
            @change="equipChange"
            class="select-device"
          >
            <el-option
              v-for="item in equipList"
              :key="item.equipId"
              :label="item.equipName"
              :value="item.equipId"
            />
          </el-select>
        </el-form-item>
        <el-form-item class="w-[100px]">
          <el-select
            v-model="searchParam.year"
            placeholder="请选择年份"
            filterable
            :suffixIcon="() => '年'"
          >
            <el-option
              v-for="item in yearOptions"
              :key="item"
              :value="item"
              :label="item"
            />
          </el-select>
        </el-form-item>
        <el-form-item class="w-[100px]">
          <el-select
            v-model="searchParam.month"
            placeholder="请选择月份"
            filterable
            :suffixIcon="() => '月'"
          >
            <el-option
              v-for="item in useDictionary('MonthList')"
              :key="item.value"
              :label="item.value"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="searchHadel"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="resetDetail"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button
          class="ml-3"
          @click="
            useDownload(
              '/eam/csmworkrecord/exportReport',
              '耗材寿命统计导出',
              searchParam
            )
          "
        >
          <template #icon><i-ep-download /></template>导出</el-button
        >
      </template>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          label="工序"
          prop="processCode"
          min-width="60"
          show-overflow-tooltip
          align="left"
        />
        <el-table-column
          label="生产线"
          prop="productCode"
          min-width="60"
          show-overflow-tooltip
          align="left"
        />
        <el-table-column
          label="设备"
          prop="productName"
          min-width="60"
          show-overflow-tooltip
          align="left"
        />
        <el-table-column
          label="设备状态监控项"
          prop="materialGroupName"
          min-width="60"
          show-overflow-tooltip
          align="left"
        />
        <el-table-column
          label="设定预警值"
          prop="materialCode"
          min-width="100"
          show-overflow-tooltip
          align="left"
        />
        <el-table-column
          label="设定报警值"
          prop="materialName"
          min-width="200"
          show-overflow-tooltip
          align="left"
        />
        <el-table-column
          label="异常最高值"
          prop="consumeCount"
          min-width="30"
          show-overflow-tooltip
          align="left"
        />
        <el-table-column
          label="达到预警时间"
          prop="workCount"
          min-width="60"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="达到报警时间"
          prop="maxCount"
          min-width="60"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="异常持续时间(h)"
          prop="repeatWorkCount"
          min-width="60"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="故障信息"
          prop="repeatMaxCount"
          min-width="60"
          show-overflow-tooltip
          align="center"
        />
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />

      <el-row style="margin-top: 10px">
        <el-col :span="8" v-for="(product, index) in 6" :key="index">
          <div style="width: 100%; height: 200px">
            <esmMonthlyPointChart
              :key="Math.random().toString"
              :id="Math.random().toString()"
            />
          </div>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { useTable, useDownload, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";
import dayjs from "dayjs";

defineOptions({
  name: "EQUESMMONTHLYREPORT",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/eam/csmworkrecord/queryReportByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  processEquipId: "", // 生产线
  pointCount: "100",
  processCode: "", // 工序编号
  processId: "", // 工序id
  // deviceCode: "", // 设备编号
  deviceId: "", // 设备id
  deviceIds: [],
  year: dayjs().year().toString(),
  month: (dayjs().month() + 1).toString(),
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError
);

onMounted(() => {
  getYears();
  getProcessEquipList();
  getEquipList();
  getProcessCodeList();
  getTableList();
});

// 获取年度列表
const yearOptions = ref();
async function getYears() {
  const { data } = await request({
    url: "/system/dateInfo/getYearList",
    method: "post",
    data: {},
  });
  yearOptions.value = data;
}
// 获取工序列表
const processCodeList: any = ref([]);
async function getProcessCodeList() {
  const { data } = await request({
    url: `/mdm/mdmprocess/getList`,
    method: "post",
    data: {},
  });
  processCodeList.value = data;
}
function processChange(code: string) {
  searchParam.value.processEquipId = "";
  searchParam.value.deviceId = "";
  searchParam.value.deviceIds = [];

  if (code) {
    const current: any =
      processCodeList.value?.filter((e: any) => e.processCode === code)[0] ??
      {};
    searchParam.value.processId = current.id || "";
  } else {
    equipList.value = [];
    searchParam.value.processId = "";
  }
  getEquipList();
  getProcessEquipList();
}

// 通过工序查询设备列表
const equipList = ref(<any>[]);
async function getEquipList(id?: string) {
  const res: any = await request({
    // url: "/iotadmin/scaequipinfo/getlist",
    url: "/dmpDosPcm/equipmentInfo/getList",
    method: "post",
    data: {
      processCode: searchParam.value.processCode,
      processId: searchParam.value.processId,
      workshopSectionId: searchParam.value.workshopSectionId,
      processEquipId: searchParam.value.processEquipId,
    },
  });
  if (res.code === 200) {
    equipList.value = res.data;
  }
}
function equipChange(ids: string) {
  //   searchParam.value.productModel = "";
}

// 获取生产线列表
const processEquipList: any = ref([]);
async function getProcessEquipList() {
  searchParam.value.processEquipId = "";
  processEquipList.value = [];
  const { data } = await request({
    // url: `/mdm/mdmprocess/getList`,
    url: "/mdm/mdmprocessequip/getList",
    method: "post",
    data: {
      workshopSectionId: searchParam.value.workshopSectionId,
      processId: searchParam.value.processId,
    },
  });
  processEquipList.value = data;
}
function processEquipChange() {
  searchParam.value.deviceId = "";
  getEquipList();
}
</script>
<style lang="scss" scoped>
:deep(.search-container) {
  .el-form-item {
    margin-right: 14px;
  }
  .el-select .el-input .el-select__caret.el-icon {
    font-style: normal;
    transform: none !important;
  }
}
</style>
